package main

import (
    "fmt"
    "github.com/gotk3/gotk3/gdk"
    "github.com/gotk3/gotk3/glib"
    "github.com/gotk3/gotk3/gtk"
    "log"
    "os"
)

func errlog(err error)  {if err!=nil {log.Fatal(err)}}
func main() {
    gtk.Init(&os.Args)

    window,err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) ;errlog(err)//新建窗口
    window.SetDefaultSize(480, 320)              //设置大小

    //鼠标按下事件处理
    window.Connect("button-press-event", func(win *gtk.Window ,evt *gdk.Event) {
        //获取鼠键按下属性结构体变量，系统内部的变量，不是用户传参变量

        event := gdk.EventButtonNewFromEvent(evt)

        if event.Type() == gdk.EVENT_BUTTON_PRESS { //单击
            fmt.Printf("单击, ")
        } else if event.Type() == gdk.EVENT_2BUTTON_PRESS { //双击
            fmt.Printf("双击, ")
        }

        //fmt.Println("button = ", event.Button)
        if event.Button() == 1 {
            fmt.Printf("左键, ")
        } else if event.Button() == 2 {
            fmt.Printf("中间键, ")
        } else if event.Button() == 3 {
            fmt.Printf("右键, ")
        }
        var stringxy string
        stringxy= fmt.Sprintf("(%d,%d)",event.X(),event.Y())
        win.SetTooltipText( stringxy)
        fmt.Println("坐标:", event.X(), event.Y())
    })

    //鼠标移动事件处理
    window.Connect("motion-notify-event", func(win *gtk.Window ,evt *gdk.Event) {
        //获取鼠标移动属性结构体变量，系统内部的变量，不是用户传参变量
        event := gdk.EventButtonNewFromEvent(evt)

        fmt.Println("移动坐标:", event.X(), event.Y())
    })

    //添加鼠标按下事件
    //BUTTON_PRESS_MASK: 鼠标按下，触发信号"button-press-event"
    //BUTTON_RELEASE_MASK：鼠标抬起，触发"button-release-event"
    //鼠标移动都是触发"motion-notify-event"
    //BUTTON_MOTION_MASK: 鼠标移动，按下任何键移动都可以
    //BUTTON1_MOTION_MASK：鼠标移动，按住左键移动才触发
    //BUTTON2_MOTION_MASK：鼠标移动，按住中间键移动才触发
    //BUTTON3_MOTION_MASK：鼠标移动，按住右键移动才触发
    //window.SetEvents(gdk.EVENT_BUTTON_PRESS  | gdk.EVENT_MOTION_NOTIFY)
    glib.TimeoutAdd(1000,func(){
         //
    })
    window.Connect("destroy", gtk.MainQuit) //关闭窗口
    window.ShowAll()                        //显示控件

    gtk.Main()
}
